// src/stores/rule.js
import { defineStore } from 'pinia'
import { ruleApi } from '@/api/rule.js'

export const useRuleStore = defineStore('rule', {
    state: () => ({
        rules: [],
        loading: false
    }),

    actions: {
        async fetchRules() {
            this.loading = true
            try {
                const response = await ruleApi.getRules()
                this.rules = response.data || []
            } finally {
                this.loading = false
            }
        },

        async createRule(rule) {
            const response = await ruleApi.createRule(rule)
            await this.fetchRules()
            return response
        },

        async updateRule(id, rule) {
            const response = await ruleApi.updateRule(id, rule)
            await this.fetchRules()
            return response
        },

        async deleteRule(id) {
            const response = await ruleApi.deleteRule(id)
            await this.fetchRules()
            return response
        },

        async toggleRule(id, enabled) {
            const response = await ruleApi.toggleRule(id, enabled)
            await this.fetchRules()
            return response
        },

        async testRules(content) {
            return await ruleApi.testRules(content)
        }
    }
})